package org.example.TOOL;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class DataMasking {

    private static final String DB_URL = "jdbc:mysql://localhost:3306/edubigdata";
    private static final String USER = "root";
    private static final String PASS = "123456";


    static final String[][] universities = {
            {"10075", "河北大学"},
            {"10076", "河北工程大学"},
            {"10077", "河北地质大学"},
            {"10080", "河北工业大学"},
            {"10081", "华北理工大学"},
            {"10082", "河北科技大学"},
            {"10084", "河北建筑工程学院"},
            {"10085", "河北水利电力学院"},
            {"10086", "河北农业大学"},
            {"10092", "河北北方学院"},
            {"10093", "承德医学院"},
            {"10094", "河北师范大学"},
            {"10096", "保定学院"},
            {"10098", "河北民族师范学院"},
            {"10099", "唐山师范学院"},
            {"10100", "廊坊师范学院"},
            {"10101", "衡水学院"},
            {"10102", "石家庄学院"},
            {"10103", "邯郸学院"},
            {"10104", "邢台学院"},
            {"10105", "沧州师范学院"},
            {"10107", "石家庄铁道大学"},
            {"10216", "燕山大学"},
            {"10798", "河北科技师范学院"},
            {"11104", "华北科技学院"},
            {"11105", "中国人民警察大学"},
            {"11236", "河北体育学院"},
            {"11420", "河北金融学院"},
            {"11629", "北华航天工业学院"},
            {"11775", "防灾科技学院"},
            {"11832", "河北经贸大学"},
            {"11903", "中央司法警官学院"},
            {"12784", "河北传媒学院"},
            {"13075", "河北美术学院"},
            {"13391", "河北科技学院"},
            {"13408", "华北理工大学轻工学院"},
            {"13411", "河北师范大学汇华学院"},
            {"13594", "河北地质大学华信学院"},
            {"13891", "保定理工学院"},
            {"13895", "燕京理工学院"},
            {"14202", "沧州交通学院"},
            {"14225", "河北东方学院"},
            {"14432", "河北中医药大学"},
            {"14458", "张家口学院"},
            {"19145", "东北大学秦皇岛分校"},
            {"51721", "河北环境工程学院"}
    };

    public static void main(String[] args) {
        List<Integer> jsghValues = new ArrayList<>();
        for (int i = 100; i <= 10000; i++) {
            jsghValues.add(i);
        }
        Collections.shuffle(jsghValues);

        try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) {
            conn.setAutoCommit(false);

            String sql = "UPDATE pd_gccch_xx SET code = ?, xxmc = ? WHERE id = ?";
            try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
                String query = "SELECT id, code, xxmc FROM pd_gccch_xx";
                try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(query)) {
                    Scanner scanner = new Scanner(System.in);
                    while (rs.next()) {
                        int id = rs.getInt("id");
                        String oldCode = rs.getString("code");
                        String oldXxmc = rs.getString("xxmc");

                        int randomIndex = (int) (Math.random() * universities.length);
                        String newCode = universities[randomIndex][0];
                        String newXxmc = universities[randomIndex][1];

                        System.out.println("原数据: code=" + oldCode + ", xxmc=" + oldXxmc);
                        System.out.println("新数据: code=" + newCode + ", xxmc=" + newXxmc);

                        System.out.println("请按Enter键确认修改...");
                       // scanner.nextLine();

                        pstmt.setString(1, newCode);
                        pstmt.setString(2, newXxmc);
                        pstmt.setInt(3, id);
                        pstmt.addBatch();
                    }
                    pstmt.executeBatch();
                    conn.commit();
                }
            } catch (SQLException e) {
                conn.rollback();
                e.printStackTrace();
                System.out.println("数据库操作失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
}
